/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.site.managedbeans;

import br.com.fatecpg.songs.dao.AlbumDAO;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.dao.util.DAOFactory;
import br.com.fatecpg.songs.entity.Album;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Eloyza
 */
@ManagedBean (name = "albumSearch")
@RequestScoped
public class AlbumManagedBean {

    private Album album;
    private final AlbumDAO albumDAO;

    public AlbumManagedBean() throws DAOException {
        albumDAO = (AlbumDAO) DAOFactory.getDAO(Album.class);
        album = getAlbumSearch();
    }

    public List<Album> getResult() {
        String search = FacesContextUtil.getParameter("search");
        if (search != null) {
            return albumDAO.search(search);
        } else {
            return new ArrayList<Album>();
        }
    }

    private Long getId() {
        if (FacesContextUtil.getParameter("idAlbum") != null) {
            Long id = Long.parseLong(FacesContextUtil.getParameter("idAlbum"));
            return id;
        }
        return null;
    }

    private Album getAlbumSearch() {
        if (getId() != null) {
            album = albumDAO.load(getId());
            return album;
        }
        return null;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

}
